package efu.nf.like.common.advice;

import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import efu.nf.like.common.exception.GlobalException;
import efu.nf.like.common.result.ResultVo;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author lwj
 * @date 2025-05-19
 * @vesion 1.0.0
 *
 * 异常捕获类
 */
@RestControllerAdvice
public class GlobalExceptionAdvice {
    /**
     * 捕获自定义异常
     *
     * @param e 自定义异常
     */
    @ExceptionHandler(GlobalException.class)
    public<T> ResultVo<T> globalExceptionHandler(GlobalException e){
        ResultVo<T> vo = new ResultVo<>();
        vo.setCode(e.getErrorCode());
        vo.setMessage(e.getMessage());
        return vo;
    }

    /**
     * 捕获全局异常 - 兜底异常
     *
     * @param e 全局异常
     */
    @ExceptionHandler(Exception.class)
    public<T> ResultVo<T> exceptionHandler(Exception e){
        ResultVo<T> vo = new ResultVo<>();
        vo.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        vo.setMessage("服务器错误");
        return vo;
    }
}